package com.chuang.server.controller;


import com.alibaba.fastjson.JSONObject;
import com.chuang.server.pojo.ResultBean;
import com.chuang.server.pojo.User;
import com.chuang.server.service.TreeCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 树形分类表
 前端控制器
 * </p>
 *
 * @author vicente
 * @since 2021-12-18
 */
@RestController
@RequestMapping("/tree-category")
public class TreeCategoryController {
    @Autowired
    private TreeCategoryService treeCategoryService;

    /**
     * 获取全部的分类数据
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    public ResultBean list(){
        return treeCategoryService.selectAll();
    }

    /**
     * 根据用户id返回该用户的所属分类
     * @param
     * @return
     */
    @ResponseBody
    @PostMapping("/byUserCategory")
    public ResultBean byUserList(@RequestBody JSONObject jsonObject){
        Long id = jsonObject.getLong("id");
        return treeCategoryService.byUserList(id);
    }

    /**
     * 增加分类
     * @param jsonObject
     * @return
     */

    @ResponseBody
    @PostMapping("/addCategory")
    public ResultBean addCategory(@RequestBody JSONObject jsonObject){
        return treeCategoryService.addCategory(jsonObject);
    }

    /**
     * 删除单个分类/删除该分类下面的子集
     * @param jsonObject
     * @return
     */
    @ResponseBody
    @PostMapping("/delCategory")
    public ResultBean delCategory(@RequestBody JSONObject jsonObject){
        return treeCategoryService.delCategory(jsonObject);
    }

    @ResponseBody
    @PostMapping("/updateCategory")
    public ResultBean updateCategory(@RequestBody JSONObject jsonObject){
        return treeCategoryService.updateCategory(jsonObject);
    }

}
